const log4js = require("log4js");
const path = require("path");
log4js.configure({
  appenders: {
    out: { type: "console" },
    access: {
      type: "dateFile",
      filename: path.resolve(__dirname, "../../logs/access.log"),
      pattern: "-yyyy-MM-dd",
    },
    app: {
      type: "file",
      filename: path.resolve(__dirname, "../../logs/app.log"),
      maxLogSize: 1048576,
      numBackups: 3,
    },
    errorFile: {
      type: "file",
      filename: path.resolve(__dirname, "../../logs/errors.log"),
    },
    errors: {
      type: "logLevelFilter",
      level: "ERROR",
      appender: "errorFile",
    },
  },
  categories: {
    default: { appenders: ["app", "errors"], level: "all" },
    logs: { appenders: ["app", "errors", "out"], level: "all" },
  },
});

module.exports = function logger(log) {
  return log ? log4js.getLogger("logs") : log4js.getLogger();
};
